using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Converters;

namespace it.mintlab.desktopnet.health360.server
{
    public class CustomWebRequest : WebRequest
    {
        private Dictionary<string, Object> args;

        public Dictionary<string, Object> Args
        {
            get { return args; }
            set { args = value; }
        }

        public CustomWebRequest(String operation, Dictionary<string, Object> args)
        {
            this.Operation = operation;
            this.args = args;
        }

        public string createJSONResponse(Object objectResponse)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("customResponse");
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("operation");
                jsonWriter.WriteValue(Operation);
                jsonWriter.WritePropertyName("result");
                // set serializer setting
                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                IsoDateTimeConverter isoDateTimeConverter = new IsoDateTimeConverter();
                isoDateTimeConverter.DateTimeFormat = "yyyy-MM-ddTHH\\:mm\\:ss";
                settings.Converters.Add(isoDateTimeConverter);
                // serialize
                jsonWriter.WriteRaw(JsonConvert.SerializeObject(objectResponse, Formatting.Indented, settings));
                
                jsonWriter.WriteEndObject();
                jsonWriter.WriteEndObject();
                jsonWriter.Flush();
            }
            return sb.ToString();
        }

    }
}
